package com.petmagics.learnmultitouch;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.EventListener;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        root = (FrameLayout) findViewById(R.id.container);
        iv = (ImageView) findViewById(R.id.iv);

        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                float currentDistance;
                float lastDistance = -1;

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("action down");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("action move");

                        if(motionEvent.getPointerCount() >= 2) {
                            float offsetX = motionEvent.getX(0) - motionEvent.getX(1);
                            float offsetY = motionEvent.getY(0) - motionEvent.getY(1);
                            currentDistance = (float) Math.sqrt(offsetX*offsetX+offsetX*offsetY);
                            if(lastDistance < 0) {
                                lastDistance = currentDistance;
                            } else {
                                if (currentDistance - lastDistance > 5) {
                                    System.out.println("放大");
                                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams();
                                    lp.width = (int) (1.1f * iv.getWidth());
                                    lp.height= (int) (1.1f * iv.getHeight());
                                    iv.setLayoutParams(lp);
                                    lastDistance = currentDistance;
                                } else if (lastDistance - currentDistance > 5) {
                                    System.out.println("缩小");
                                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams();
                                    lp.width= (int) (0.9f * iv.getWidth());
                                    lp.height= (int) (0.9f * iv.getHeight());
                                    iv.setLayoutParams(lp);
                                    lastDistance = currentDistance;
                                }
                            }
                        }


//                        System.out.println("pointer count: "+motionEvent.getPointerCount());
//                        System.out.println(String.format("x1:%f y1:%f x2:%f y2:%f", motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(1), motionEvent.getY(1)));
//                        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams();
//                        lp.leftMargin = (int) motionEvent.getX();
//                        lp.topMargin = (int) motionEvent.getY();
//                        iv.setLayoutParams(lp);
//                        System.out.println(String.format("x:%f,y:%f", motionEvent.getX(), motionEvent.getY()));
                        break;
                    case MotionEvent.ACTION_UP:
                        System.out.println("action up");
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }

    private FrameLayout root;
    private ImageView iv;
}
